{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Curve Overlay Context\n",
    "Scenario contexts enable the users to price and calculate risk using their own curves."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.instrument import IRSwaption\n",
    "from gs_quant.risk import MarketDataPattern, MarketDataShock, MarketDataShockType, MarketDataShockBasedScenario, \\\n",
    "    RollFwd, CurveScenario, IndexCurveShift, CurveOverlay\n",
    "from gs_quant.session import Environment, GsSession\n",
    "from datetime import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# external users should substitute their client id and secret; please skip this step if using internal jupyterhub\n",
    "GsSession.use(Environment.PROD, client_id=None, client_secret=None, scopes=('run_analytics',))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original price: 605,255.51\n"
     ]
    }
   ],
   "source": [
    "# Create and price a swaption\n",
    "swaption = IRSwaption('Pay', '5y', 'USD', expiration_date='3m')\n",
    "base_price = swaption.price()\n",
    "print('Original price: {:,.2f}'.format(base_price))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Curve Overlay Scenario \n",
    "A predefined scenario used to overlay discount curve or index curve. This allows the user to use customized discount factors to overwrite existing curves on graph.\n",
    "* discount_factors - Discount factors\n",
    "* dates - Dates of the discount factors.\n",
    "* curve_type - Discount curve or index curve.\n",
    "* rate_option - Rate option of the index curve.\n",
    "* tenor - Tenor of the index curve.\n",
    "* csa_term - The funding scenario of the curve.\n",
    "* denominated - The denominated currency of the curve.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Scenario price: 635,392.60\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# Read discount factors from a csv file\n",
    "df = pd.read_csv (r'CurveExample.csv', sep=\",\" )\n",
    "dates = df.MaturityDate.tolist()\n",
    "dates_reformat = [ datetime.strptime(date, \"%d-%b-%Y\").strftime(\"%Y-%m-%d\") for date in dates ]\n",
    "discount_factors = df.DiscountFactor.tolist()\n",
    "\n",
    "curve_overlay_scenario = CurveOverlay(\n",
    "    dates = dates_reformat,\n",
    "    discount_factors = discount_factors,\n",
    "    denominated = \"USD\",\n",
    "    csa_term = \"USD-SOFR\",\n",
    "    curve_type = \"Discount Curve\"\n",
    ")\n",
    "with curve_overlay_scenario:\n",
    "    price_with_overlay = swaption.price()\n",
    "\n",
    "print('Scenario price: {:,.2f}'.format(price_with_overlay))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
